package com.example.demo.rest.account;

import com.example.demo.api.account.AwsAdminApi;
import com.example.demo.api.account.AzureAdminApi;
import com.example.demo.api.account.OpenApi;
import com.example.demo.bean.charts.ChartsRes;
import com.example.demo.bean.charts.PieChart;
import com.example.demo.util.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;


@RestController
public class SyncController {
    @Autowired
    AzureAdminApi azureAdminApi;
    @Autowired
    AwsAdminApi awsAdminApi;

    @Autowired
    OpenApi openApi;

    @GetMapping("/sync/switch")
    public Map<String, String> syncSwitch() {
        Map<String, String> map = new HashMap<>();
        String azureToAws = GsonUtils.getVal(azureAdminApi.getDoubleWriteOperationStatus(), "status");
        String azureToFlash = GsonUtils.getVal(azureAdminApi.operationStatus(), "status");
        String awsToAzure = GsonUtils.getVal(awsAdminApi.getDoubleWriteOperationStatus(), "status");
        String flashToAzure = GsonUtils.getVal(openApi.changeToggleStatus(), "status");
        map.put("azureToAwsStatus", azureToAws);
        map.put("azureToFlashStatus", azureToFlash);
        map.put("awsToAzureStatus", awsToAzure);
        //map.put("flashToAzureStatus", flashToAzure);
        return map;
    }

    @GetMapping("/sync/sevenDayStatistics")
    public ChartsRes sevenDayStatistics(@RequestParam("type") String type) {
        ChartsRes chartsRes = new ChartsRes();
        TreeMap<String, Integer[]> stringMap = azureAdminApi.statisticsByType(type,7);
        List<String> categories = new ArrayList<>();
        List<Integer> type1 = new ArrayList<>();
        List<Integer> type2 = new ArrayList<>();
        List<Integer> type3 = new ArrayList<>();
        List<Integer> type4 = new ArrayList<>();
        List<ChartsRes.CDate> cDates = new ArrayList<>();
        stringMap.forEach((k, v) -> {
            categories.add(k);
            type1.add(v[0]);
            type2.add(v[1]);
            type3.add(v[2]);
            type4.add(v[3]);
        });

        cDates.add(new ChartsRes.CDate("个人信息", type1));
        cDates.add(new ChartsRes.CDate("头像", type2));
        cDates.add(new ChartsRes.CDate("新增地址", type3));
        cDates.add(new ChartsRes.CDate("删除地址", type4));
        chartsRes.setCategories(categories);
        chartsRes.setCDates(cDates);
        return chartsRes;
    }

    @GetMapping("/sync/todayStatistics")
    public ChartsRes todayStatistics() {
        ChartsRes chartsRes = new ChartsRes();
        TreeMap<String, Integer[]> stringMapSuccess = azureAdminApi.statisticsByType("success",1);
        TreeMap<String, Integer[]> stringMapError = azureAdminApi.statisticsByType("error",1);

        List<String> categories = new ArrayList<>(Arrays.asList("个人信息","头像","新增地址","删除地址"));

        List<ChartsRes.CDate> cDates = new ArrayList<>();

        stringMapSuccess.forEach((k,v)-> cDates.add(new ChartsRes.CDate("成功", Arrays.asList(v))));
        stringMapError.forEach((k,v)-> cDates.add(new ChartsRes.CDate("失败", Arrays.asList(v))));

        chartsRes.setCategories(categories);
        chartsRes.setCDates(cDates);
        return chartsRes;
    }

    @GetMapping("/sync/getStatistics")
    public PieChart getStatistics(@RequestParam("type") String type) {
        PieChart pieChart = new PieChart();
        Map<String, Integer> statistics = azureAdminApi.getStatistics(type);
        List<PieChart.PieData> pieData = statistics.entrySet().stream().map(k -> new PieChart.PieData(k.getKey(), k.getValue().doubleValue())).collect(Collectors.toList());
        pieChart.setData(pieData);
        return pieChart;
    }
}
